﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Yd.Model;
using Yd.Domain.Abstract;
namespace Yd.Domain.DAL
{
    public class CommonDal:BaseCls
    {
        public static DateTime addTradingDays(DateTime startDate,int days)
        {
            object endDate = DbHelperSQL.GetSingle("select dbo.addTradingDays('"+startDate+"',"+days+")");

            return (DateTime) endDate;
        }

        public DataSet getPageList(int pageSize, int pageIndex ,string table, string fields ,string strWhere, string order, out int total)
        {
            //Execute Proce_Sql_PageChange 'tz_caiwu','*','Id desc',@wheresql,@PageSize,@PageIndex,@t output
            int _count = 0;

            using (SqlConnection connection = new SqlConnection(ConStr))
            {
                try
                {
                    DataSet ds = new DataSet();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "Proce_Sql_PageChange";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = connection;
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@TableName", SqlDbType.NVarChar, 50);
                        cmd.Parameters.Add("@ReFieldsStr", SqlDbType.NVarChar, 200);
                        cmd.Parameters.Add("@OrderString", SqlDbType.NVarChar, 200);
                        cmd.Parameters.Add("@WhereString", SqlDbType.NVarChar, 200);
                        cmd.Parameters.Add("@PageSize", SqlDbType.Int, 4);
                        cmd.Parameters.Add("@PageIndex", SqlDbType.Int, 4);

                        cmd.Parameters["@TableName"].Value = table;
                        cmd.Parameters["@ReFieldsStr"].Value = fields;
                        cmd.Parameters["@OrderString"].Value = order;
                        cmd.Parameters["@WhereString"].Value = strWhere;
                        cmd.Parameters["@PageSize"].Value = pageSize;
                        cmd.Parameters["@PageIndex"].Value = pageIndex;

                        connection.Open();
                        using (SqlDataAdapter da =  new SqlDataAdapter(cmd))
                        {
                            da.Fill(ds);
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        total = _count;
                    }
                    return ds;
                }
                catch (Exception e)
                {
                    total = _count;
                    LocalLog(e.Message);
                    return null;
                }
                finally
                {
                    connection.Close();
                }
            }
            
        }
    }
}
